package kpi.javatraining6.gnatiuk.src.tester.user;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.NoSuchElementException;

import kpi.javatraining6.gnatiuk.src.teachingsystem.Difficulty;
import kpi.javatraining6.gnatiuk.src.tester.question.AnsweredQuestion;
import kpi.javatraining6.gnatiuk.src.tester.question.Question;


public class UserProgress implements Serializable{

	private static final long serialVersionUID = 1L;

    private ArrayList<AnsweredQuestion> answeredQuestions;
    private int rightnessCounter;
    private Difficulty currentDifficulty;

	public UserProgress() {
        rightnessCounter = 0;
        currentDifficulty = Difficulty.NORMAL;
        answeredQuestions = new ArrayList<>();
	}

    public UserProgress(ArrayList<AnsweredQuestion> answeredQuestions) {
        this.answeredQuestions = answeredQuestions;
        evaluateRightnessCounter();
        evaluateDifficulty();
    }


    private void evaluateRightnessCounter() {
        rightnessCounter = 0;
        for (AnsweredQuestion answeredQuestion: answeredQuestions) {
            if (answeredQuestion.isRightAnswer()) {
                rightnessCounter += 1;
            }
        }
    }

	public void addToAnswered(Question question, ArrayList<String> answerArray){
        boolean userAnswer = question.answerTheQuestion(answerArray);
        AnsweredQuestion answeredQuestion = new AnsweredQuestion(question.getQuestion(), userAnswer);
        answeredQuestions.add(answeredQuestion);
        updateDifficulty(userAnswer);
	}

	public void removeLastAnswered(){
        if (answeredQuestions.size() < 1) {
            return;
        }
        int lastIndex = answeredQuestions.size() - 1;
        AnsweredQuestion lastOne = answeredQuestions.get(lastIndex);
        updateDifficulty(!lastOne.isRightAnswer());
        answeredQuestions.remove(lastIndex);
	}

    public int getAnsweredQuestionsNumber() {
        return answeredQuestions.size();
    }

    private void updateDifficulty(boolean rightAnswer) {
        if (rightAnswer) {
            rightnessCounter++;
        }else {
            rightnessCounter--;
        }
        evaluateDifficulty();
    }

    public AnsweredQuestion getAnsweredQuestion(int index) {
        if (index < 0 || index >= answeredQuestions.size()) {
            throw new NoSuchElementException();
        }
        return answeredQuestions.get(index);
    }

    public Difficulty getCurrentDifficulty() {
        return currentDifficulty;
    }

    private void evaluateDifficulty() {
        int hardQuestionsLimit = 2;
        int easyQuestionsLimit = -2;
        if (rightnessCounter > hardQuestionsLimit) {
            currentDifficulty = Difficulty.HARD;
        } else if (rightnessCounter < easyQuestionsLimit){
            currentDifficulty = Difficulty.EASY;
        } else {
            currentDifficulty = Difficulty.NORMAL;
        }
    }

	public boolean isAnswered(Question question){
        for (AnsweredQuestion answeredQuestion: answeredQuestions) {
            if (question.getQuestion().equals(answeredQuestion.getQuestion())) {
                return true;
            }
        }
        return false;
	}

	public String getStatistic(){
		int score = 0;
		for (AnsweredQuestion question : answeredQuestions) {
            if (question.isRightAnswer()) {
			    score += 1;
            }
		}
		return String.format("%d/%d", score, answeredQuestions.size());
	}

	@Override
	public String toString() {
		return String.format("UserProgress [answeredQuestions = " + "]");
	}
	
}
